package com.varian.auth.util;

import cn.hutool.core.lang.Assert;
import com.varian.auth.constant.AuthConstant;
import com.varian.redis.util.RedisUtil;
import com.varian.tool.exception.ServerException;
import lombok.experimental.UtilityClass;

import java.util.concurrent.TimeUnit;

/**
 * @author ben
 * @since 2024/7/30
 */
@UtilityClass
public class CaptchaUtil {
    public void cacheCode(String uuid, String code) {
        RedisUtil.Value.set(AuthConstant.KEY_CAPTCHA.format(uuid), code, 2, TimeUnit.MINUTES);
    }

    public boolean matches(String uuid, String code) {
        String rawCode = RedisUtil.Value.getAndDel(AuthConstant.KEY_CAPTCHA.format(uuid));
        Assert.notBlank(rawCode, () -> ServerException.of("验证码已失效"));
        return rawCode.equals(code);
    }
}
